
import CryptoJS from 'crypto-js';

function initAes (keyStr, n) {
  if (keyStr.length < n) {
    throw new Error('n过大');
  }
  return keyStr.substr(n).concat(keyStr.substr(0, n));
}

// 加密
function encrypt (word, aesKey) {
  const key = CryptoJS.enc.Utf8.parse(aesKey),
    srcs = CryptoJS.enc.Utf8.parse(word),
    encrypted = CryptoJS.AES.encrypt(srcs, key, {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    });
  return encrypted.toString();
}

// 解密
function decrypt (word, aesKey) {
  const key = CryptoJS.enc.Utf8.parse(aesKey),
    decrypt = CryptoJS.AES.decrypt(word, key, {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    });
  return CryptoJS.enc.Utf8.stringify(decrypt).toString();
}
// 解密
function decode (word) {
  return decrypt(word, 'fdb413dcc2c243eebab91f64e8759c58');
}

function encode (word) {
  return encrypt(word, 'fdb413dcc2c243eebab91f64e8759c58');
}
export default {
  initAes,
  encrypt,
  decode,
  encode
};
